package com.fantastic.service.lwf.impl;

import com.fantastic.common.utils.FilesUtils;
import com.fantastic.mapper.lwf.SetMealInfoMapper;
import com.fantastic.mapper.zjs.VarietySpecsMapper;
import com.fantastic.model.pojo.lwf.SetMeal;
import com.fantastic.model.vo.lwf.app.setmeal.FoodLists;
import com.fantastic.model.vo.lwf.app.setmeal.SetMealVo;
import com.fantastic.model.vo.zjs.VarietySpecsVo;
import com.fantastic.service.lwf.SetMealInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;

/**
 * @author .FengYe.
 * @date 2024-06-18
 */
@Service
public class SetMealInfoServiceImpl implements SetMealInfoService {

    @Resource
    private VarietySpecsMapper varietySpecsMapper;

    @Resource
    private SetMealInfoMapper setMealInfoMapper;

    @Override
    public SetMealVo getSetMealVoBymId(int mId) {
        // 先获取它套餐的菜品信息
        SetMeal setMeal = setMealInfoMapper.getSetMeal(mId);
        setMeal.setVId(setMealInfoMapper.getSetMealFoodsIdBymId(mId));
        // 所有菜品信息
        ArrayList<VarietySpecsVo> variety = new ArrayList<>();
        for (Integer i : setMeal.getVId()) {
            List<VarietySpecsVo> varietySpecsVos = varietySpecsMapper.listVarietySpecsVo(i);

            if (!varietySpecsVos.isEmpty()) {
                VarietySpecsVo varietySpecsVo = varietySpecsVos.get(0);
                varietySpecsVo.setVarietyPicture(FilesUtils.getSlashParse(varietySpecsVo.getVarietyPicture()));
                variety.add(varietySpecsVo);
            }
        }

        // 实例化 套餐vo
        SetMealVo setMealVo = new SetMealVo();
        setMealVo.setMId(setMeal.getMealId())
                .setMName(setMeal.getMealName())
                .setMPrice(setMeal.getMealPrice())
                .setMDepict(setMeal.getMealDepict())
                .setMChoice(setMeal.getMealChoice());

        // 实例化 菜品信息的分类
        ArrayList<FoodLists> foodLists = new ArrayList<>();

        // 这个套餐中的所有菜品
        for (VarietySpecsVo varietySpecsVo : variety) {
            // 实例化
            FoodLists lists = null;
            // 判断集合是否为空
            if (foodLists.isEmpty()) {

                lists = new FoodLists();
                // 塞入第一条数据
                lists.setFoodTypeId(varietySpecsVo.getVarietyTypeId())
                        .setFoodTypeName(varietySpecsVo.getVarietyTypeName());

                // 初始化 菜品信息
                ArrayList<VarietySpecsVo> varietySpecsVos = new ArrayList<>();
                // 循环菜品信息
                for (VarietySpecsVo specsVo : variety) {
                    // 找到类型的菜品
                    if (lists.getFoodTypeId().equals(specsVo.getVarietyTypeId())) {
                        varietySpecsVos.add(specsVo);
                    }
                }
                lists.setFoods(varietySpecsVos);
            } else {
                boolean foodType = false;
                // 循环菜品信息的分类
                for (FoodLists foodList : foodLists) {
                    // 判断 是否存在这条类型
                    if (varietySpecsVo.getVarietyTypeId().equals(foodList.getFoodTypeId())) {
                        foodType = true;
                        break;
                    }
                }
                // 不存在才进行添加
                if (!foodType) {
                    lists = new FoodLists();
                    lists.setFoodTypeId(varietySpecsVo.getVarietyTypeId())
                            .setFoodTypeName(varietySpecsVo.getVarietyTypeName());

                    // 初始化 菜品信息
                    ArrayList<VarietySpecsVo> varietySpecsVos = new ArrayList<>();
                    // 循环菜品信息
                    for (VarietySpecsVo specsVo : variety) {
                        // 找到类型的菜品
                        if (lists.getFoodTypeId().equals(specsVo.getVarietyTypeId())) {
                            varietySpecsVos.add(specsVo);
                        }
                    }
                    lists.setFoods(varietySpecsVos);
                }
            }

            if (lists != null) {
                foodLists.add(lists);
            }
        }

        setMealVo.setFoodLists(foodLists);

        return setMealVo;
    }

    @Override
    public SetMeal test(int mId) {
        SetMeal setMeal = setMealInfoMapper.getSetMeal(mId);
        setMeal.setVId(setMealInfoMapper.getSetMealFoodsIdBymId(mId));
        return setMeal;
    }

}
